package com.macro.mall.controller;

import com.macro.mall.common.api.CommonPage;
import com.macro.mall.common.api.CommonResult;
import com.macro.mall.common.vo.CurCourseBaseResp;
import com.macro.mall.model.CurCourseBase;
import com.macro.mall.service.CurCourseBaseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 课程基础表(cur_course_base）
 *
 * @author gaodx
 * @create 2020-08-11 15:43
 **/
@Slf4j
@RestController
@RequestMapping("curCourseBase")
@Api(tags = "CurCourseBaseController", description = "课程基础表(cur_course_base）")
public class CurCourseBaseController {


    @Autowired
    private CurCourseBaseService curCourseBaseService;

    @ApiOperation("查询课程")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<CommonPage<CurCourseBaseResp>> list(@RequestParam(value = "courseName", required = false) String courseName,
                                                            @RequestParam(value = "courseType", required = false) Integer courseType,
                                                            @RequestParam(value = "deptName", required = false) String deptName,
                                                            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                            @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize) {
        List<CurCourseBaseResp> bycondition = curCourseBaseService.findBycondition(courseName, courseType, deptName, pageNum, pageSize);
        return CommonResult.success(CommonPage.restPage(bycondition));
    }


    @ApiOperation("添加课程")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult save(@RequestBody CurCourseBase request) {
        try {
            curCourseBaseService.saveNotNull(request);
            return CommonResult.success(null);
        } catch (Exception e) {
            log.error("",e);
        }
        return CommonResult.failed();

    }

    @ApiOperation("查询课程详情")
    @RequestMapping(value = "/getById", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<CurCourseBaseResp> getById(@ApiParam(value = "id") @RequestParam(value = "id", required = true) Long id) {
        try {
            CurCourseBaseResp one = curCourseBaseService.findOne(id);
            return CommonResult.success(one);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return CommonResult.failed();
    }


    @ApiOperation("删除课程详情")
    @RequestMapping(value = "/delById", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult deleteById(@ApiParam(value = "id") @RequestParam(value = "id", required = true) Long id) {
        try {
            CurCourseBase curCourseBase = new CurCourseBase();
            curCourseBase.setId(id);
            curCourseBase.setStatus(0);
            curCourseBaseService.updateNotNullById(curCourseBase);
            return CommonResult.success(null);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return CommonResult.failed();

    }

    @ApiOperation("更新课程信息")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult update(@ApiParam(value = "请求报文") @RequestBody CurCourseBase request) {
        try {
            curCourseBaseService.updateNotNullById(request);
            return CommonResult.success(null);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return CommonResult.failed();

    }















}